<?php
/**
 * Formular dns zaznamu
 * @author Tomas Srna
 * @package bakalarka
 */

class HostingFtpForm extends Form 
{
	public function __construct(Template $template, $values = NULL, 
		$action = NULL, $method = 'POST')
	{
		parent::__construct($template, $values, $action, $method);
		
		$this->items = array(
			new Element('username', L::selected()->username, "text", 
						"required albegin nospace".(
							$this->values instanceof Model || 
							array_key_exists('id', $_GET) 
							? " readonly" : "")
						),
			new Element('root', L::selected()->root, "text", 
						"nospace path", L::selected()->ftpRootHint, NULL, 100),
		);
	}
	
	/**
	 * Osobitna validacia
	 */
	public function validate()
	{
		parent::validate();
		
		if(!$this->getError('username') &&
			!preg_match('/^[a-z0-9_-]+$/i', 
				$this->getValue('username')))
		{
			$this->setError('username', 
				sprintf(L::selected()->invalidFormat_s, 
					'a-z, A-Z, 0-9, _ -'));
		}
		
		if(!$this->getError('root'))
		{
			$parts = explode("/", $this->getValue('root'));
			if(array_search('..', $parts) !== false)
			{
				$this->setError('root', 
					sprintf(L::selected()->invalidFormat_sn, 
					'..'));
			}
		}
		
		// Nesmie zacinat /
		if(!$this->getError('root') && 
			substr($this->getValue('root'), 0, 1) == '/')
		{
			$this->setError('root',
				L::selected()->noSlashBegin);
		}
		
		if(count($this->errors) > 0)
			return false;
		
		return true;
	}
}
